package com.team.common.dynamic.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class CustomerTypeFilter implements TypeFilter {

    private Class<?> clz;

    public CustomerTypeFilter(Class<?> clz){
        this.clz = clz;
    }

    @Override
    public boolean match(MetadataReader metadataReader,
                         MetadataReaderFactory metadataReaderFactory) throws IOException {

        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        if(classMetadata.isInterface() || classMetadata.isAnnotation()){
            return false;
        }
        String[] interfaces = classMetadata.getInterfaceNames();
        String inter = clz.getCanonicalName();
        if(interfaces == null || interfaces.length == 0){
            return false;
        }
        for(String it : interfaces){
            if(StringUtils.equals(it,inter)){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Class<?> c = CustomerTypeFilter.class;
        System.out.println(c.getName());
        System.out.println(c.getCanonicalName());
        System.out.println(c.getSimpleName());
    }
}
